package com.poi.tl.sample.quickstart.app.tags;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.Includes;
import com.poi.tl.sample.quickstart.consts.AppConsts;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.springframework.core.io.ClassPathResource;

public class _7NestedTemplateExport {

  public static void main(String[] args) throws Exception {
    ClassPathResource templateFile = new ClassPathResource(AppConsts.TAG_TEMPLATE_CLASS_PATH + "_7_1main_template.docx");
    ClassPathResource subTemplateFile = new ClassPathResource(AppConsts.TAG_TEMPLATE_CLASS_PATH + "_7_1sub_template.docx");
    String outFile = AppConsts.EXPORT_PATH + "_7_1main_template_output.docx";

    // 1 	compile 编译模板
    XWPFTemplate template = XWPFTemplate.compile(templateFile.getInputStream()).render(
        // 2 render 渲染数据
        new HashMap<String, Object>(){{
          List<AddrModel> subData = new ArrayList<>();
          subData.add(new AddrModel("Hangzhou,China"));
          subData.add(new AddrModel("Shanghai,China"));
          put("nested", Includes.ofLocal(subTemplateFile.getFile().getPath()).setRenderModel(subData).create());
        }});
    // 3 write 输出到流
    template.writeAndClose(new FileOutputStream(outFile));
  }

  static class AddrModel {

    private String addr;

    public AddrModel(String addr) {
      this.addr = addr;
    }

    public String getAddr() {
      return addr;
    }

    public void setAddr(String addr) {
      this.addr = addr;
    }
  }

}

